/**
  ******************************************************************************
  * @文件名           : dri_AnalogQuantity.c
  * @作者             : citunhk
  * @功能介绍         : 模拟量驱动
  *	@日期             : 2022.11.1
  ******************************************************************************
  * @注意
  *
  *
  *
  ******************************************************************************
  *更改记录
  *日期 修改人 修改内容
  *
  *
  *
  ******************************************************************************
  */
#include "dri_AnalogQuantity.h"
#include "bsp_adc.h"
#include "bsp_pwm.h"
#include "sys_data.h"
#include "dri_flash_sst.h"

#define PT100_F50_TO_F1			1
#define PT100_0_TO_49				1
#define PT100_50_TO_149			1
#define PT100_150_TO_299		0
#define PT100_300_TO_499		0

#define PT100_ARRAY_SIZE	 (PT100_F50_TO_F1*50+PT100_0_TO_49*50+PT100_50_TO_149*100+PT100_150_TO_299*150+PT100_300_TO_499*200)

RCF RCF_Vadc[NUM_OF_ADC] = {/* ADC滤波参数结构 */
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		}
};
RCF RCF_PV[NUM_OF_ADC] = {/* 物理量滤波参数结构 */
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 100,
				.err_max = 30,
				.k_inc = 1,
				.k_init = 0,
				.k_max = 125
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 50,
				.err_max = 20,
				.k_inc = 2,
				.k_init = 0,
				.k_max = 150
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		},
		{
				.lastValue = 0,
				.lastDirection = 0,
				.cnt_max = 10,
				.err_max = 10,
				.k_inc = 20,
				.k_init = 10,
				.k_max = 255
		}
};
float gFilter_adc[NUM_OF_ADC];  //处理后的AD值

SensorParam SR_Param;

float g_fMainTemp_PID1;
float g_fMainTemp_PID2;
float g_fMainTemp_PID3;
//float Temp_Value1;
//float Temp_Value2;
//float Temp_Value3;
float ResRpt1;
float ResRpt2;
float ResRpt3;
float fLowPass1;
float fLowPass2;
float fLowPass3;
float DisTempValue1;
float DisTempValue2;
float DisTempValue3;
float CaliTemp_K[2];
float CaliTemp_B[2];
float CaliTemp1_K[2];
float CaliTemp1_B[2];
float CaliTemp2_K[2];
float CaliTemp2_B[2];
float CaliTemp3_K[2];
float CaliTemp3_B[2];
float Rpt1_K[5];
float Rpt1_B[5];
float Rpt2_K[5];
float Rpt2_B[5];
float Rpt3_K[5];
float Rpt3_B[5];

const uint16_t pt100_table[PT100_ARRAY_SIZE] ={
#if PT100_F50_TO_F1
8031,	8070,	8110,	8150,	8189,	8229,	8269,	8308,	8348,	8387,		// -50 -- -41
8427,	8467,	8506,	8546,	8585,	8625,	8664,	8704,	8743,	8783,		// -40 -- -31
8822,	8862,	8901,	8940,	8980,	9019,	9059,	9098,	9137,	9177,		// -30 -- -21
9216,	9255,	9295,	9334,	9373,	9412,	9452,	9491,	9530,	9569,		// -20 -- -11
9609,	9648,	9687,	9726,	9765,	9804,	9844,	9883,	9922,	9961,		// -10 --  -1
#endif
#if PT100_0_TO_49
10000,	10039,	10078,	10117,	10156,	10195,	10234,	10273,	10312,	10351,		//   0 --   9
10390,	10429,	10468,	10507,	10546,	10585,	10624,	10663,	10702,	10740,		//  10 --  19
10779,	10818,	10857,	10896,	10935,	10973,	11012,	11051,	11090,	11129,		//  20 --  29
11167,	11206,	11245,	11283,	11322,	11361,	11400,	11438,	11477,	11515,		//  30 --  39
11554,	11593,	11631,	11670,	11708,	11747,	11786,	11824,	11863,	11901,		//  40 --  49
#endif
#if PT100_50_TO_149
11940,	11978,	12017,	12055,	12094,	12132,	12171,	12209,	12247,	12286,		//  50 --  59
12324,	12363,	12401,	12439,	12478,	12516,	12554,	12593,	12631,	12669,		//  60 --  69
12708,	12746,	12784,	12822,	12861,	12899,	12937,	12975,	13013,	13052,		//  70 --  79
13090,	13128,	13166,	13204,	13242,	13280,	13318,	13357,	13395,	13433,		//  80 --  89
13471,	13509,	13547,	13585,	13623,	13661,	13699,	13737,	13775,	13813,		//  90 --  99
13851,	13888,	13926,	13964,	14002,	14040,	14078,	14116,	14154,	14191,		// 100 -- 109
14229,	14267,	14305,	14343,	14380,	14418,	14456,	14494,	14531,	14569,		// 110 -- 119
14607,	14644,	14682,	14720,	14757,	14795,	14833,	14870,	14908,	14946,		// 120 -- 129
14983,	15021,	15058,	15096,	15133,	15171,	15208,	15246,	15283,	15321,		// 130 -- 139
15358,	15396,	15433,	15471,	15508,	15546,	15583,	15620,	15658,	15695,		// 140 --149
#endif
#if PT100_150_TO_299
15733,	15770,	15807,	15845,	15882,	15919,	15956,	15994,	16031,	16068,		// 150 -- 159
16105,	16143,	16180,	16217,	16254,	16291,	16329,	16366,	16403,	16440,		// 160 -- 169
16477,	16514,	16551,	16589,	16626,	16663,	16700,	16737,	16774,	16811,		// 170 -- 179
16848,	16885,	16922,	16959,	16996,	17033,	17070,	17107,	17143,	17180,		// 180 -- 189
17217,	17254,	17291,	17328,	17365,	17402,	17438,	17475,	17512,	17549,		// 190 -- 199
17586,	17622,	17659,	17696,	17733,	17769,	17806,	17843,	17879,	17916,		// 200 -- 209
17953,	17989,	18026,	18063,	18099,	18136,	18172,	18209,	18246,	18282,		// 210 -- 219
18319,	18355,	18392,	18428,	18465,	18501,	18538,	18574,	18611,	18647,		// 220 -- 229
18684,	18720,	18756,	18793,	18829,	18866,	18902,	18938,	18975,	19011,		// 230 -- 239
19047,	19084,	19120,	19156,	19192,	19229,	19265,	19301,	19337,	19374,		// 240 -- 249
19410,	19446,	19482,	19518,	19555,	19591,	19627,	19663,	19699,	19735,		// 250 -- 259
19771,	19807,	19843,	19879,	19915,	19951,	19987,	20023,	20059,	20095,		// 260 -- 269
20131,	20167,	20203,	20239,	20275,	20311,	20347,	20383,	20419,	20455,		// 270 -- 279
20490,	20526,	20562,	20598,	20634,	20670,	20705,	20741,	20777,	20813,		// 280 -- 289
20848,	20884,	20920,	20956,	20991,	21027,	21063,	21098,	21134,	21170,		// 290 -- 299
#endif
#if PT100_300_TO_499
21205,	21241,	21276,	21312,	21348,	21383,	21419,	21454,	21490,	21525,		// 300 -- 309
21561,	21596,	21632,	21667,	21703,	21738,	21774,	21809,	21844,	21880,		// 310 -- 319
21915,	21951,	21986,	22021,	22057,	22092,	22127,	22163,	22198,	22233,		// 320 -- 329
22268,	22304,	22339,	22374,	22409,	22445,	22480,	22515,	22550,	22585,		// 330 -- 339
22621,	22656,	22691,	22726,	22761,	22796,	22831,	22866,	22902,	22937,		// 340 -- 349
22972,	23007,	23042,	23077,	23112,	23147,	23182,	23217,	23252,	23287,		// 350 -- 359
23321,	23356,	23391,	23426,	23461,	23496,	23531,	23566,	23600,	23635,		// 360 -- 369
23670,	23705,	23740,	23774,	23809,	23844,	23879,	23913,	23948,	23983,		// 370 -- 379
24018,	24052,	24087,	24122,	24156,	24191,	24226,	24260,	24295,	24329,		// 380 -- 389
24364,	24399,	24433,	24468,	24502,	24537,	24571,	24606,	24640,	24675,		// 390 -- 399
24709,	24744,	24778,	24813,	24847,	24881,	24916,	24950,	24985,	25019,		// 400 -- 409
25053,	25088,	25122,	25156,	25191,	25225,	25259,	25293,	25328,	25362,		// 410 -- 419
25396,	25430,	25465,	25499,	25533,	25567,	25601,	25635,	25670,	25704,		// 420 -- 429
25738,	25772,	25806,	25840,	25874,	25908,	25942,	25976,	26010,	26044,		// 430 -- 439
26078,	26112,	26146,	26180,	26214,	26248,	26282,	26316,	26350,	26384,		// 440 -- 449
26418,	26452,	26486,	26520,	26553,	26587,	26621,	26655,	26689,	26722,		// 450 -- 459
26756,	26790,	26824,	26857,	26891,	26925,	26959,	26992,	27026,	27060,		// 460 -- 469
27093,	27127,	27161,	27194,	27228,	27261,	27295,	27329,	27362,	27396,		// 470 -- 479
27429,	27463,	27496,	27530,	27563,	27597,	27630,	27664,	27697,	27731,		// 480 -- 489
27764,	27798,	27831,	27864,	27898,	27931,	27964,	27998,	28031,	28064			// 490 -- 499
#endif
};

const uint16_t pu16ptR[10] = {8101,9001,10001,11001,12001,13001,14001,15001,16001,17001};  //需要校准的电阻点

float ppt1_K[9];  //PT100-1校准K
float ppt1_B[9];  //PT100-1校准B

float ppt2_K[9];  //PT100-2校准K
float ppt2_B[9];  //PT100-2校准B

float ppt3_K[9];  //PT100-3校准K
float ppt3_B[9];  //PT100-3校准B

float pai1_K[2];  //模拟量输入1校准K
float pai1_B[2];  //模拟量输入1校准B
float pai2_K[2];  //模拟量输入2校准K
float pai2_B[2];  //模拟量输入2校准B
float pai3_K[2];  //模拟量输入3校准K
float pai3_B[2];  //模拟量输入3校准B
float pai4_K[2];  //模拟量输入4校准K
float pai4_B[2];  //模拟量输入4校准B
float gpres_ele;


/**
* @函数名	DRI_Save_Restoru
* @功能	    温度与流量参数恢复出厂
* @入口参数	无
* @返回值	无
*/
void DRI_Save_Restoru(void)
{
	//温度1校准AD
	sav.Para.pptAD1[0] = 915;
    sav.Para.pptAD1[1] = 1338;
    sav.Para.pptAD1[2] = 1809;
	sav.Para.pptAD1[3] = 2275;
	sav.Para.pptAD1[4] = 2738;
	sav.Para.pptAD1[5] = 3197;
	sav.Para.pptAD1[6] = 3654;
    sav.Para.pptAD1[7] = 4091;

	//温度2校准AD
	sav.Para.pptAD2[0] = 915;
    sav.Para.pptAD2[1] = 1338;
    sav.Para.pptAD2[2] = 1809;
	sav.Para.pptAD2[3] = 2275;
	sav.Para.pptAD2[4] = 2738;
	sav.Para.pptAD2[5] = 3197;
	sav.Para.pptAD2[6] = 3654;
    sav.Para.pptAD2[7] = 4091;

	//温度3校准AD
	sav.Para.pptAD3[0] = 915;
    sav.Para.pptAD3[1] = 1338;
    sav.Para.pptAD3[2] = 1809;
	sav.Para.pptAD3[3] = 2275;
	sav.Para.pptAD3[4] = 2738;
	sav.Para.pptAD3[5] = 3197;
	sav.Para.pptAD3[6] = 3654;
    sav.Para.pptAD3[7] = 4091;

    //流量校准上下限  相应AD值
//    FP.Para.LL_FlowPV = 0;
//	FP.Para.HL_FlowPV = 16;
	sav.Para.pptAD1[8] = 769;  //4mA
//	sav.Para.pptAD1[8] = 1154;  //6mA
	sav.Para.pptAD1[9] = 3856;  //20mA

	//压力校准AD
//	sav.Para.paiAD[0] = 769;  //4mA
//	sav.Para.paiAD[1] = 3856;  //20mA
	sav.Para.paiAD[0] = 196;  //0.5V
    sav.Para.paiAD[1] = 1742;  //4.5V

    //水质校准AD
    sav.Para.paiAD[2] = 1154;
	sav.Para.paiAD[3] = 3856;
}

/**
* @函数名	DRI_KB_Init
* @功能	    温度参数初始化
* @入口参数	无
* @返回值	无
*/
void DRI_KB_Init(void)
{
	uint8_t j;

	//温度1KB初始化
	for(j=0;j<7;j++)
	{
		ppt1_K[j] = (float)(pu16ptR[j+1]-pu16ptR[j])/(float)(sav.Para.pptAD1[j+1]-sav.Para.pptAD1[j]);
		ppt1_B[j] = (float)pu16ptR[j]-((float)sav.Para.pptAD1[j]*ppt1_K[j]);
	}

	//温度2KB初始化
//	for(j=0;j<7;j++)
//	{
//		ppt2_K[j] = (float)(pu16ptR[j+1]-pu16ptR[j])/(float)(sav.Para.pptAD2[j+1]-sav.Para.pptAD2[j]);
//		ppt2_B[j] = (float)pu16ptR[j]-((float)sav.Para.pptAD2[j]*ppt2_K[j]);
//	}

	for(j=0;j<7;j++)
	{
		ppt2_K[j] = (float)(pu16ptR[j+1]-pu16ptR[j])/(float)(sav.Para.pptAD1[j+1]-sav.Para.pptAD1[j]);
		ppt2_B[j] = (float)pu16ptR[j]-((float)sav.Para.pptAD1[j]*ppt2_K[j]);
	}

	//温度3KB初始化
//	for(j=0;j<7;j++)
//	{
//		ppt2_K[j] = (float)(pu16ptR[j+1]-pu16ptR[j])/(float)(sav.Para.pptAD2[j+1]-sav.Para.pptAD2[j]);
//		ppt2_B[j] = (float)pu16ptR[j]-((float)sav.Para.pptAD2[j]*ppt2_K[j]);
//	}

	for(j=0;j<7;j++)
	{
		ppt3_K[j] = (float)(pu16ptR[j+1]-pu16ptR[j])/(float)(sav.Para.pptAD1[j+1]-sav.Para.pptAD1[j]);
		ppt3_B[j] = (float)pu16ptR[j]-((float)sav.Para.pptAD1[j]*ppt2_K[j]);
	}

	//流量AD初始化
	pai1_K[0] = (float)(FP.Para.HL_FlowPV-FP.Para.LL_FlowPV)/(float)(sav.Para.pptAD1[9]-sav.Para.pptAD1[8]);
	pai1_B[0] = (float)FP.Para.LL_FlowPV-((float)sav.Para.pptAD1[8]*pai1_K[0]);

	//出液压力AD初始化
	pai2_K[0] = (float)(FP.Para.HL_PressPV-FP.Para.LL_PressPV)/(float)(sav.Para.paiAD[1]-sav.Para.paiAD[0]);
	pai2_B[0] = (float)FP.Para.LL_PressPV-((float)sav.Para.paiAD[0]*pai2_K[0]);

	//回液压力AD初始化
	pai3_K[0] = (float)(FP.Para.HL_PressPV-FP.Para.LL_PressPV)/(float)(sav.Para.paiAD[1]-sav.Para.paiAD[0]);
	pai3_B[0] = (float)FP.Para.LL_PressPV-((float)sav.Para.paiAD[0]*pai3_K[0]);

	//水质AD初始化
	pai4_K[0] = (float)(FP.Para.HL_FlowPV-FP.Para.LL_FlowPV)/(float)(sav.Para.paiAD[3]-sav.Para.paiAD[2]);
	pai4_B[0] = (float)FP.Para.LL_FlowPV-((float)sav.Para.paiAD[2]*pai4_K[0]);
}

/**
* @函数名	DRI_MedianFilter
* @功能	    中值滤波
* @入口参数	pData[] 滤波数组
*           size 滤波数据个数
* @返回值	滤波数值
*/
uint16_t DRI_MedianFilter(uint16_t pData[], uint8_t size)
{
	uint16_t stmp;

    if(size>2)
    {
        for(uint8_t i = 0; i < size - 1; i++)
        {
        	for(uint8_t j = 0; j < size- i - 1; j++)
        	{
        		if(pData[j] > pData[j+1])
        		{
        			stmp = pData[j];
        			pData[j] = pData[j+1];
        			pData[j+1] = stmp;
        		}
        	}
        }
    }
    return pData[size/2];
}

/**
* @函数名	DRI_RcFilter
* @功能	    ADC值低通滤波
* @入口参数	pRCF 滤波结构体
*           fValueInput 滤波系数
* @返回值	滤波数值
*/
float DRI_RcFilter(RCF *pRCF,float nowValue)
{
	float err = 0;
	uint8_t flag_diffDir = 0;

	if(nowValue > pRCF->lastValue)//本次AD增加
	{
		err = nowValue - pRCF->lastValue;

		if(pRCF->lastDirection == 0)//如果上次是减少,方向标志置为ture
			flag_diffDir = 1;
		pRCF->lastDirection = 1;//更新方向为增加
	}
	else if(nowValue < pRCF->lastValue)//本次AD减少
	{
		err = pRCF->lastValue - nowValue;

		if(pRCF->lastDirection == 1)//如果上次是增加,方向标志置为ture
			flag_diffDir = 1;
		pRCF->lastDirection = 0;//更新方向为减少
	}

	if(flag_diffDir)//方向不一致,权重置为0(即RC=0),直接忽略本次采样
	{
		pRCF->k = pRCF->k_init;
		pRCF->cnt = 0;
	}
	else	//方向一致
	{
		pRCF->cnt++;//计数器加1
		if(err > pRCF->err_max)//输入变化量>阀值,计数器再加2
		{
			pRCF->cnt += 2;
		}

		if(pRCF->cnt > pRCF->cnt_max)//计数器达到最大值
		{
			pRCF->k += pRCF->k_inc;//滤波系数+增量
			if(pRCF->k > pRCF->k_max)
				pRCF->k = pRCF->k_max;
			pRCF->cnt = 0;
		}
	}

	return pRCF->lastValue = pRCF->lastValue + (nowValue - pRCF->lastValue) * (pRCF->k / 255.0);

}

/**
* @函数名	DRI_Temperature_Pt100Cal
* @功能	    根据当前pt100的电阻值，返回对应的温度值
* @入口参数	pt100_res pt100的电阻值
* @返回值	当前的温度值，放大10倍
*/
float DRI_Temperature_Pt100Cal(uint16_t pt100_res)
{
	int8_t h=0,l=0;
	float temper_int = 0;		//温度的整数部分
	float temper_dot = 0;		//温度的小数部分

	if( pt100_res < pt100_table[0] || pt100_res > pt100_table[199] )		//温度的范围
	{
		return -510;		//超出温度范围，温度输出为-5.1
	}
	for(h=0; h<55; h++)		//先查列表,共55列数据
	{
		if(pt100_res < pt100_table[10*h])
		{
			break;
		}
	}

	for(l=0; l<10; l++)		//再查行表
	{
		if(pt100_res < pt100_table[10 * (h - 1) + l])
		{
			break;
		}
	}

	temper_int = 10 * (h - 1) + l - 1;	//温度的整数部分
	temper_int = 10 * temper_int;		//整数部分放大10倍
	temper_dot = 10 * (float)(pt100_res - pt100_table[10*(h-1)+l-1]) / (float)(pt100_table[10*(h-1)+l] - pt100_table[10*(h-1)+l-1]);

	return temper_int+temper_dot-500;
}

/**
* @函数名	DRI_ScalingConversion
* @功能	    等比缩放换算 4-20mA转换为电压，再装换为AD值，再转换为测量值
* @入口参数	ad:ad值
*       	dataMax:测量值最大值
*       	dataMin:测量值最小值
*       	Cali:采集电阻值 默认为500.0欧
* @返回值	无
*/
float DRI_ScalingConversion(uint16_t ad, int16_t pvMax, int16_t pvMin, float Cali)
{
	return ((float)ad * 3300 * (pvMax - pvMin) ) / (Cali * 16 * 4095) - (0.25 *  (pvMax - pvMin)) + pvMin;
}

/**
* @函数名	DRI_Analog_In
* @功能	    将AD值滤波
* @入口参数	无
* @返回值	无
*/
void DRI_Analog_In(void)
{
	static uint8_t pRC_Cnt=0;
	uint16_t pAnalog_in__ad[NUM_OF_ADC][SIZE_OF_ONE_DMA];		    //模拟量输入第一路AD值
	uint16_t * p ;
	uint16_t pAnalogInAdcpv[NUM_OF_ADC];  //模拟量滤波后输出AD值

	p = &pAnalog_in__ad[0][0];
	uint16_t j = 0;
	//采集DMA中的AD值
	for(uint16_t i = 0; i < SIZE_OF_ALL_DMA; i++)
	{
		*p++ = gAD_DMAbuffer[(i - j * SIZE_OF_ONE_DMA) * NUM_OF_ADC+ j];
		if(i % (SIZE_OF_ONE_DMA) == (SIZE_OF_ONE_DMA) - 1)
		{
			j++;
		}
	}

	//采用中值平均滤波的方式，去除高斯噪音
	for(uint8_t cnt = 0; cnt < NUM_OF_ADC; cnt++)
	{
		pAnalogInAdcpv[cnt] = DRI_MedianFilter(pAnalog_in__ad[cnt], SIZE_OF_ONE_DMA);
	}
	//低通滤波
	if(pRC_Cnt<6)
	{
		pRC_Cnt++;
		for(uint8_t cnt = 0; cnt < NUM_OF_ADC; cnt++)
		{
			gFilter_adc[cnt] = RCF_Vadc[cnt].lastValue = pAnalogInAdcpv[cnt];  //上一步滤波结果
		}
	}
	else
	{
		for(uint8_t cnt = 0; cnt < NUM_OF_ADC; cnt++)
		{
			gFilter_adc[cnt]=DRI_RcFilter(&RCF_Vadc[cnt], pAnalogInAdcpv[cnt]);
		}
	}
}

/**
* @函数名	DRI_Calibration_AD
* @功能	    AD校准
* @入口参数	无
* @返回值	无
*/
void DRI_Calibration_AD(void)
{
	uint8_t i,j;

	for(i = 0; i < 10; i++)
	{
		if(gSTATE_TempCalibration & ((0x0001) << i))
		{
			gSTATE_TempCalibration = 0;
			if(i < 8)
			{
				sav.Para.pptAD1[i] = gFilter_adc[0];
				for(j = 0; j < 7; j++)
				{
				  ppt1_K[j] = (float)(pu16ptR[j+1] - pu16ptR[j]) / (float)(sav.Para.pptAD1[j+1] - sav.Para.pptAD1[j]);
				  ppt1_B[j] = (float)pu16ptR[j] - ((float)sav.Para.pptAD1[j] * ppt1_K[j]);
				}
			}
			else
			{
				sav.Para.pptAD1[i] = gFilter_adc[6];
				pai1_K[0] = (float)(FP.Para.HL_FlowPV - FP.Para.LL_FlowPV) / (float)(sav.Para.pptAD1[9] - sav.Para.pptAD1[8]);
				pai1_B[0] = (float)FP.Para.LL_FlowPV - ((float)sav.Para.pptAD1[8] * pai1_K[0]);
			}
			DRI_Flash_Erase_4KByte(ADDR_SAVE);//写入前擦除
			DRI_Flash_AAI_WriteBuffer(&sav.flashBuff[0], ADDR_SAVE, 100);//写入系统参数初值到buff中
		}
	}
}

/**
* @函数名	PT100_Read_Temp
* @功能	    输出PT100实际温度
* @入口参数	pt100ch PT100通道号 1
*                               2
*                               3
* @返回值	ptemper PT100实际温度值 放大10倍
*/
float DRI_Read_Temp(uint8_t pt100ch)
{
	uint16_t pres_cur = 0;  //计算的电阻值
	uint8_t i;
	float ptemper = 0;  //返回温度值
	float Temp_Value;  //温度计算值
	float g_fMainTemp_PID;
	static uint8_t pbFirstGet_PID1= 0,pbFirstGet_PID2= 0,pbFirstGet_PID3= 0;    //第一次采集
	static float pCalculateTemp=0,pLastMainTemp2=0,pLastMainTemp3=0,pLastPidTemp1=0,pLastPidTemp2=0,pLastPidTemp3=0;
//	static float pLastMainTemp1=0;
	float fLowPass;

	switch (pt100ch)
	{
		case 1:
			/*****************根据ADC的值，转换成当前的温度值*****************/
			for(i = 0; i < 7; i++)
			{
				if(gFilter_adc[0] >= sav.Para.pptAD1[i] && gFilter_adc[0] < sav.Para.pptAD1[i+1])
				{
					pres_cur = ppt1_K[i] * gFilter_adc[0] + ppt1_B[i];
					break;//退出循环
				}
			}

			pCalculateTemp = DRI_Temperature_Pt100Cal(pres_cur);  //电阻转换成温度值
			if(pbFirstGet_PID1==0)
			{
				pbFirstGet_PID1 = 1;
				RCF_PV[0].lastValue = pLastPidTemp1 = Temp_Value = pCalculateTemp;
			}
			else
			{
				/* 20240419增加滤波 开始 */
				Temp_Value = DRI_RcFilter(&RCF_PV[0], pCalculateTemp);
				/* 20240419增加滤波 结束 */
//				fLowPass=0.005;
//				Temp_Value=pCalculateTemp*fLowPass+pLastMainTemp1*(1-fLowPass);
				if((Temp_Value<0)&&(Temp_Value>-0.001))
				{
					Temp_Value=0;
//					pLastMainTemp1=pCalculateTemp;
				}
//				else{pLastMainTemp1=Temp_Value;}
				g_fMainTemp_PID=pCalculateTemp*0.9+pLastPidTemp1*0.1;
				g_fMainTemp_PID = UP.Para.CaliTemp1_K*g_fMainTemp_PID + 10*UP.Para.CaliTemp1_B;
				pLastPidTemp1=g_fMainTemp_PID;
				gPV_TempPID1 = g_fMainTemp_PID/10;
			}

			//温度偏差校准
			Temp_Value = UP.Para.CaliTemp1_K*Temp_Value + 10*UP.Para.CaliTemp1_B;
			break;
		case 2:
			/*****************根据ADC的值，转换成当前的温度值*****************/
			for(i = 0; i < 7; i++)
			{
				if(gFilter_adc[1] >= sav.Para.pptAD1[i] && gFilter_adc[1] < sav.Para.pptAD1[i+1])
				{
					pres_cur = ppt2_K[i] * gFilter_adc[1] + ppt2_B[i];
					break;//退出循环
				}
			}

			pCalculateTemp = DRI_Temperature_Pt100Cal(pres_cur);  //电阻转换成温度值
			if(pbFirstGet_PID2==0)
			{
				pbFirstGet_PID2 = 1;
				Temp_Value=pCalculateTemp ;
				pLastMainTemp2=Temp_Value;
				pLastPidTemp2=Temp_Value;
			}
			else
			{
				fLowPass=0.1;
				Temp_Value=pCalculateTemp*fLowPass+pLastMainTemp2*(1-fLowPass);
				if((Temp_Value<0)&&(Temp_Value>-0.001))
				{
					Temp_Value=0;
					pLastMainTemp2=pCalculateTemp;
				}
				else{pLastMainTemp2=Temp_Value;}

				g_fMainTemp_PID=pCalculateTemp*0.9+pLastPidTemp2*0.1;
				pLastPidTemp2=g_fMainTemp_PID;
			}

			//温度偏差校准
			Temp_Value = UP.Para.CaliTemp2_K*Temp_Value + 10*UP.Para.CaliTemp2_B;
			break;
		case 3:
			/*****************根据ADC的值，转换成当前的温度值*****************/
			for(i = 0; i < 7; i++)
			{
				if(gFilter_adc[2] >= sav.Para.pptAD1[i] && gFilter_adc[2] < sav.Para.pptAD1[i+1])
				{
					pres_cur = ppt3_K[i] * gFilter_adc[2] + ppt3_B[i];
					break;//退出循环
				}
			}

			pCalculateTemp = DRI_Temperature_Pt100Cal(pres_cur);  //电阻转换成温度值
			if(pbFirstGet_PID3==0)
			{
				pbFirstGet_PID3 = 1;
				Temp_Value=pCalculateTemp ;
				pLastMainTemp3=Temp_Value;
				pLastPidTemp3=Temp_Value;
			}
			else
			{
				fLowPass=0.1;
				Temp_Value=pCalculateTemp*fLowPass+pLastMainTemp3*(1-fLowPass);
				if((Temp_Value<0)&&(Temp_Value>-0.001))
				{
					Temp_Value=0;
					pLastMainTemp3=pCalculateTemp;
				}
				else{pLastMainTemp3=Temp_Value;}

				g_fMainTemp_PID=pCalculateTemp*0.9+pLastPidTemp3*0.1;
				pLastPidTemp3=g_fMainTemp_PID;
			}

			//温度偏差校准
			Temp_Value = UP.Para.CaliTemp3_K*Temp_Value + 10*UP.Para.CaliTemp3_B;
			break;
   }

	return ptemper = Temp_Value;
}

/**
* @函数名	DRI_Read_Flow
* @功能	    读取流量
* @入口参数	flowch 流量传感器通道号 1
*                                  2
*                                  3
*                                  4
* @返回值	pFlow 实际流量值
*/
float DRI_Read_Flow(uint8_t flowch)
{
	float pFlow = 0;
	static float pLastFlow1 = 0;

	switch (flowch)
	{
		case 1:
			pFlow = pai1_K[0] * gFilter_adc[6] + pai1_B[0];  //获取流量

			pFlow = 0.3*pFlow + 0.7*pLastFlow1;
			pLastFlow1 = pFlow;
			//流量偏差校准
			pFlow = UP.Para.CaliFlow_K*pFlow + UP.Para.CaliFlow_B;
			//小信号切除
			if((pFlow > -(FP.Para.LL_FlowPV + 0.05*FP.Para.HL_FlowSV)) && (pFlow < (FP.Para.LL_FlowPV + 0.05*FP.Para.HL_FlowSV)))
			{
				pFlow = 0.0;
			}
//			if((pFlow > -1) && (pFlow <= 2))
//			{
//				pFlow = 0.0;
//			}

			break;
		case 2:
			pFlow = pai2_K[0] * gFilter_adc[5] + pai2_B[0];  //获取出液压力
//			pFlow = DRI_ScalingConversion(gFilter_adc6,FP.Para.HL_PressPV,FP.Para.LL_PressPV,500);  //获取压力
			pFlow = DRI_RcFilter(&RCF_PV[4], pFlow);
			//出液压力偏差校准
			pFlow = UP.Para.CaliPress_K1*pFlow + UP.Para.CaliPress_B1;

			//小信号切除
			if((pFlow > -(FP.Para.LL_PressPV + 0.05*FP.Para.HL_PressPV)) && (pFlow < (FP.Para.LL_PressPV + 0.05*FP.Para.HL_PressPV)))
			{
				pFlow = 0.0;
			}

			break;
		case 3:
			pFlow = pai3_K[0] * gFilter_adc[4] + pai3_B[0];  //获取回液压力
//			pFlow = DRI_ScalingConversion(gFilter_adc5,FP.Para.HL_PressPV,FP.Para.LL_PressPV,500);  //获取压力

			//回液压力偏差校准
			pFlow = UP.Para.CaliPress_K2*pFlow + UP.Para.CaliPress_B2;

			//小信号切除
			if((pFlow > -(FP.Para.LL_PressPV + 0.05*FP.Para.HL_PressPV)) && (pFlow < (FP.Para.LL_PressPV + 0.05*FP.Para.HL_PressPV)))
			{
				pFlow = 0.0;
			}
			break;
		case 4:
			pFlow = pai4_K[0] * gFilter_adc[3] + pai4_B[0];  //获取水质
			//水质偏差校准
			pFlow = UP.Para.CaliDI_K*pFlow + UP.Para.CaliDI_B;

			//小信号切除
			if(pFlow > -0.5 && pFlow < 0.5)
			{
				pFlow = 0.0;
			}

			break;
   }

	return pFlow;
}

/**
* @函数名	DRI_Analog_Output
* @功能	    输出PWM
* @入口参数	way 模拟量输出通道选择 ONE_AOUT
*                                 TWO_AOUT
*           dutyCycle 具体输出值 0-4095
* @返回值	无
*/
void DRI_Analog_Output(outCh_t way,uint16_t dutyCycle)
{
	switch(way)
	{
	case ONE_AOUT:
			LL_TIM_OC_SetCompareCH1(TIM15,dutyCycle);
			break;
	case TWO_AOUT:
			LL_TIM_OC_SetCompareCH2(TIM15,dutyCycle);
			break;
	default:
		break;
	}
}
